package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dp.appi.exception.CommonException;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import org.hibernate.exception.DataException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;


/**
 * Created by XMWEILOVE on 2016/6/8.
 */
@Controller
@RequestMapping(value = "/checkStatisticsControl")
public class CheckStatisticsControl {
    private static Logger log = LoggerFactory.getLogger(CheckStatisticsControl.class);

    @Autowired
    private CheckStatisticsService checkStatisticsService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ConsumerService consumerService;

    @Autowired
    private ExplorationService explorationService;
    //checkProject

    /**
     * 项目检查
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/checkProject", method = RequestMethod.GET)
    public void checkProject(ModelMap modelMap, @RequestParam Map<String, String> param) {
        Map<String, Object> checkProjectResult = new HashMap<>();
        String stage_id = param.get("stage_id");

        try {
            Map<String, Object> projectMap = projectService.projectInfo(Long.valueOf(stage_id));

                //数据权限类型
                String openIdent = param.get("openIdent");
                UserEntity userEntity = UserEntity.decrypt(openIdent);
                Map<String, Object> map = consumerService.getUser(userEntity);
                Integer classify = explorationService.getClassify(Long.valueOf(stage_id), String.valueOf(map.get("id")));
                param.put("classify", String.valueOf(classify));
                //连接关系检查
                Map<String, Object> joinCon = new HashMap<>();
                List<Map<String, Object>> joinConList = checkStatisticsService.joinCon(param);
                joinCon.put("result", joinConList.size() == 0 ? "符合" : "不符合");
                joinCon.put("list", joinConList);
                checkProjectResult.put("joinCon", joinCon);

                //典设连接关系
                Map<String, Object> codeset = new HashMap<>();
                List<Map<String, Object>> codesetList = checkStatisticsService.codeset(param);
                codeset.put("result", codesetList.size() == 0 ? "符合" : "不符合");
                codeset.put("list", codesetList);
                checkProjectResult.put("codeset", codeset);

//                //档距
//                Map<String, Object> span = new HashMap<>();
//                List<Map<String, Object>> spanList = checkStatisticsService.span(param);
//                span.put("result", spanList.size() == 0 ? "符合" : "不符合");
//                span.put("list", spanList);
//                checkProjectResult.put("span", span);

//                //供电半径
//                Map<String, Object> supplyRadius = new HashMap<>();
//                List<Map<String, Object>> supplyRadiusList = checkStatisticsService.supplyRadius(param);
//                supplyRadius.put("result", supplyRadiusList.size() == 0 ? "符合" : "不符合");
//                supplyRadius.put("list", supplyRadiusList);
//                checkProjectResult.put("supplyRadius", supplyRadius);

            modelMap.addAllAttributes(checkProjectResult);
        } catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

}
